package com.example.eventhandletest;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

/**
 * 这个工程用于对事件传递，和view测量绘制机制
 * com.example.eventhandletest.MainActivity
 * @author zhangyw
 * create at 2015年3月30日 下午2:08:09
 */
public class MainActivity extends ActionBarActivity {

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    if (savedInstanceState == null) {
      Fragment a =new PlaceholderFragment();
      a.hasOptionsMenu();
      getSupportFragmentManager().beginTransaction().add(R.id.container, a).commit();
    }
  }
  
  
  
  /* (non-Javadoc)
   * @see android.view.ViewGroup#dispatchTouchEvent(android.view.MotionEvent)
   */
  @Override
  public boolean dispatchTouchEvent(MotionEvent ev) {
    Log.v("MainActivity", "dispatchTouchEvent::"+" begin");
    boolean rst=super.dispatchTouchEvent(ev);
    Log.v("MainActivity", "dispatchTouchEvent::"+MotionEvent.actionToString(ev.getAction())+rst);
    return rst;
  }
  /* (non-Javadoc)
   * @see android.view.View#onTouchEvent(android.view.MotionEvent)
   */
  @Override
  public boolean onTouchEvent(MotionEvent event) {
    Log.v("MainActivity", "onTouchEvent::"+" begin");
    boolean rst=super.onTouchEvent(event);
//    if(event.getAction()==MotionEvent.ACTION_DOWN){
//      rst=true;
//    }
    Log.v("MainActivity", "onTouchEvent::"+MotionEvent.actionToString(event.getAction())+rst);
    return rst;
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
  }

  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) {
      return true;
    }
    return super.onOptionsItemSelected(item);
  }

  /**
   * A placeholder fragment containing a simple view.
   */
  public static class PlaceholderFragment extends Fragment {

    public PlaceholderFragment() {
      setHasOptionsMenu(true);
    }
    
    
    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onContextItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onContextItemSelected(MenuItem item) {
      
      return super.onContextItemSelected(item);
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onOptionsItemSelected(android.view.MenuItem)
     */
    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
      if(item.getItemId()==R.id.action_settings||item.getItemId()==R.id.f_add){
        TextView a =new ShowEventTextView(getActivity());
        a.setText("new yaeh");
        ((MyLinearLayout)getView().findViewById(R.id.alinear)).addView(a);
        return true;
      }
      return super.onOptionsItemSelected(item);
    }
    
    /* (non-Javadoc)
     * @see android.support.v4.app.Fragment#onCreateOptionsMenu(android.view.Menu, android.view.MenuInflater)
     */
    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
      super.onCreateOptionsMenu(menu, inflater);
      inflater.inflate(R.menu.frag_menu, menu);
    }
    
    
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
      View rootView = inflater.inflate(R.layout.fragment_main, container, false);
      return rootView;
    }
  }
}
